﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using Qiang.Data;
using Qiang.Topology.Geometries;

using Point = System.Drawing.Point;

namespace Qiang.Controls
{
    public class MapDrawArgs
    {

        /// <summary>
        /// 图层绘制各自内容时传的参数
        /// </summary>
        /// <param name="g"></param>
        /// <param name="r"></param>
        /// <param name="extent"></param>
        public MapDrawArgs(Graphics g, Rectangle r, IExtent extent)
        {
            this._mapGraphics = g;
            this._mapRectangle = r;
            this._mapExtent = extent;
        }


        private Graphics _mapGraphics;

        /// <summary>
        /// 临时背景绘图的画笔
        /// </summary>
        public Graphics MapGraphics
        {
            get { return _mapGraphics; }
            set { _mapGraphics = value; }
        }

        private Rectangle _mapRectangle;

        /// <summary>
        /// 临时绘图区域的大小（像素）
        /// </summary>
        public Rectangle MapRectangle
        {
            get { return _mapRectangle; }
            set { _mapRectangle = value; }
        }


        private IExtent _mapExtent;

        /// <summary>
        /// 临时绘图区域的大小（米）
        /// </summary>
        public IExtent MapExtent
        {
            get { return _mapExtent; }
            set { _mapExtent = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public double Dx
        {
            get { return this._mapRectangle.Width / this._mapExtent.Width; }
        }

        /// <summary>
        /// 
        /// </summary>
        public double Dy
        {
            get { return this._mapRectangle.Height / this._mapExtent.Height; }
        }

        /// <summary>
        /// 绝对坐标转换成屏幕坐标
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        public Point ProjToPixel(Coordinate pt)
        {

            if (pt == null) return new Point(0, 0);
            if (this._mapExtent == null) return new Point(0, 0);
            if (this._mapRectangle == null) return new Point(0, 0);

            double x = pt.X;
            double y = pt.Y;

            x = this._mapRectangle.X + (x - this._mapExtent.MinX) * Dx;
            y = this._mapRectangle.Y + (this._mapExtent.MaxY - y) * Dy;

            return new Point((int)x, (int)y);
        }


        public Rectangle ProjToPixel(Extent ext)
        {
            Point leftUp = this.ProjToPixel(new Coordinate(ext.MinX, ext.MaxY));
            Point rightBottom = this.ProjToPixel(new Coordinate(ext.MaxX, ext.MinY));
            return new Rectangle(leftUp.X, leftUp.Y, rightBottom.X - leftUp.X, rightBottom.Y - leftUp.Y);
        }


        public Coordinate PixelToProj(Point pt)
        { 
            return null ;
        }

    }
}
